let mongo;

module.exports = (app) => {

    if (mongo) {
        return mongo;
    }

    const mongoose = require("mongoose");
    // 链接mongodb数据库
    mongoose.connect(app.config.database, {useNewUrlParser: true, useUnifiedTopology: true});

    mongoose.connection.on('connected', function () {
        app.logger.info('connect mongodb successful');
        app.logger.debug('connect mongodb successful');
    });

    mongoose.connection.on('error', function () {
        app.logger.info('connect mongodb failed');
        app.logger.debug('connect mongodb failed');
    });

    mongoose.connection.on('disconnected', function () {
        app.logger.info('disconnect mongodb');
        app.logger.debug('disconnect mongodb');
    });

    mongo = mongoose;

    return mongo;
};
